import 'package:casdon/modules/lifeCircle/domain/usecase/life_blogger_info_usecase.dart';
import 'package:casdon/modules/me/data/model/user_center_model.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class LifeBloggerInfoNotifier extends Notifier<UserCenterModel> {
  String? _currentUserId; // 存储当前显示的 userId

  Future<UserCenterModel> fetchData(String userId) async {
    _currentUserId = userId; // 保存 userId
    final usercenterApi = await ref
        .read(lifeBloggerInfoUsecaseProvider)
        .execute(userId: userId);
    if (usercenterApi == null) {
      state = UserCenterModel.initial();
    } else {
      state = UserCenterModel.fromApi(usercenterApi);
    }

    return state;
  }

  @override
  UserCenterModel build() {
    return UserCenterModel.initial();
  }

  /// 更新关注状态
  /// [targetUserId] 被关注/取消关注的用户ID
  /// [isFollowed] 新的关注状态
  void updateFollowStatus(String targetUserId, bool isFollowed) {
    // 只有当目标用户ID匹配当前显示的 userId 时才更新
    if (_currentUserId == targetUserId) {
      final currentState = state;
      state = UserCenterModel(
        followTotal: currentState.followTotal,
        fansTotal: currentState.fansTotal,
        topicTotal: currentState.topicTotal,
        isFollow: isFollowed,
      );
    }
  }
}

final lifeBloggerInfoNotifierProvider =
    NotifierProvider<LifeBloggerInfoNotifier, UserCenterModel>(() {
      return LifeBloggerInfoNotifier();
    });
